<template>
  <div style="background:white; width: 700px; margin: 60px auto; padding: 20px 50px">
    <h1 style="margin: 30px auto;text-align: center;">用户注册</h1>
    <div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="用户名称" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"  show-password></el-input>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input v-model="ruleForm.age"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phonenum">
          <el-input v-model="ruleForm.phonenum"></el-input>
        </el-form-item>

        <div style="margin-left: 180px">
          <template>
            性别：
            <el-radio v-model="ruleForm.gender" label="0" @click="changGender(0)">女</el-radio>
            <el-radio v-model="ruleForm.gender" label="1" @click="changGender(0)">男</el-radio>
          </template>
        </div>


        <div style="margin-top:30px;margin-left: 160px ">
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </div>

      </el-form>
    </div>
  </div>
</template>


<style>
body {
  background: url("/src/assets/空境.jpg");
}
</style>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
        age: '',
        phonenum: '',
        gender: '0'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'},
          {pattern: /^([0-9]|[1-9][0-9]|(1[01][0-9]|120))*$/, min: 1, max: 3, message: '请输入一个0-120之间的整数', trigger: 'blur'}
        ],
        phonenum: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          {pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, message: '请输入正确的电话号码', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请选择性别', trigger: 'blur'},

        ]
      },
    }
  },
  methods: {
    changGender(a){
      this.gender=a;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/user/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000 ) {
              this.$message({
                showClose: true,
                message: '添加用户成功！',
                type: 'success'
              });
              this.$router.push('/');
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>